﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hartley.Entities;
using Hartley.DTOs;

namespace Hartley.BusinessLogic
{
    public class AthleticManager
    {
        HCPPADataContext hcDc;

        public AthleticManager(HCPPADataContext hcDc)
        {
            this.hcDc = hcDc;
        }


        public string SaveAthleticEvent(AthleticEventDTO athleticEventDTO)
        {
            string result = "";
            var athEvent = hcDc.hcAthleticEvents.Where(n => n.AthleticEventId == athleticEventDTO.AthleticEventId).SingleOrDefault();
            if (athEvent == null)
            {
                var athEvnt = hcDc.hcAthleticEvents.Where(a => a.AgeDivision == athleticEventDTO.AgeDivision && a.EventCategory == athleticEventDTO.EventCategory).SingleOrDefault();
                if (athEvnt == null)
                {

                    hcAthleticEvent athleticEvent = new hcAthleticEvent
                    {
                        AthleticEventId = athleticEventDTO.AthleticEventId,
                        EventCategory = athleticEventDTO.EventCategory,
                        AgeDivision = athleticEventDTO.AgeDivision,
                        Description = athleticEventDTO.Description,
                        ModifiedBy = athleticEventDTO.ModifiedBy,
                        ModifiedTime = athleticEventDTO.ModifiedTime
                    };
                    hcDc.hcAthleticEvents.InsertOnSubmit(athleticEvent);
                    result = "Saved";
                }
                else
                {
                    //Don't add same event
                }


            }
            else
            {
                //Edit
                result = "Updated";
            }

            hcDc.SubmitChanges();
            return result;
        }


        public void SaveAthleticData(AthleticDataDTO athleticDataDTO)
        {
            var athData = hcDc.hcAthleticDatas.Where(n => n.AthleticEventId == athleticDataDTO.AthleticEventId
                && n.StudentId == athleticDataDTO.StudentId && n.Year == athleticDataDTO.Year && n.LevelName == athleticDataDTO.LevelName).SingleOrDefault();
            if (athData == null)
            {
                hcAthleticData athleticData = new hcAthleticData
                {
                    AthleticEventId = athleticDataDTO.AthleticEventId,
                    StudentId = athleticDataDTO.StudentId,
                    Year = athleticDataDTO.Year,
                    Place = athleticDataDTO.Place,
                    LevelName = athleticDataDTO.LevelName,
                    ModifiedBy = athleticDataDTO.ModifiedBy,
                    ModifiedTime = athleticDataDTO.ModifiedTime
                };
                hcDc.hcAthleticDatas.InsertOnSubmit(athleticData);

            }

            hcDc.SubmitChanges();
        }




        public void SaveAthleticRecord(AthleticRecordDTO athleticRecordDTO)
        {
            var athData = hcDc.hcRecords.Where(n => n.RecordId == athleticRecordDTO.RecordId).SingleOrDefault();
            if (athData == null)
            {
                hcRecord athleticRecord = new hcRecord
                {
                    RecordId = athleticRecordDTO.RecordId,
                    AthleticEventId = athleticRecordDTO.AthleticEventId,
                    StudentId = athleticRecordDTO.StudentId,
                    Year = athleticRecordDTO.Year,
                    TimeOrDistance = athleticRecordDTO.TimeOrDistance,
                    PreviousTimeOrDistance = athleticRecordDTO.PreviousTimeOrDistance,
                    //PreviousStudentId = athleticRecordDTO.PreviousStudentId,
                    //LevelName = athleticRecordDTO.LevelName,
                    ModifiedTime = athleticRecordDTO.ModifiedTime,
                    ModifiedBy = athleticRecordDTO.ModifiedBy
                };
                hcDc.hcRecords.InsertOnSubmit(athleticRecord);

            }

            hcDc.SubmitChanges();
        }


        public List<AthleticEventDTO> GetAthleticEventById(Guid athleticEventId)
        {
            return hcDc.hcAthleticEvents.Where(a => a.AthleticEventId == athleticEventId).Select(s => new AthleticEventDTO
            {
                AthleticEventId = s.AthleticEventId,
                EventName = s.AgeDivision + " , " + s.EventCategory,
                AgeDivision = s.AgeDivision,
                EventCategory = s.EventCategory,
                Description = s.Description
            }).ToList();

        }




        public List<AthleticEventDTO> GetAthleticEvents()
        {
            return hcDc.hcAthleticEvents.Select(s => new AthleticEventDTO
            {
                AthleticEventId = s.AthleticEventId,
                EventName = s.AgeDivision + " , " + s.EventCategory,
                NavigationUrl = "viewAthleticDataById.aspx?" + s.AthleticEventId,
                AgeDivision = s.AgeDivision,
                Description = s.Description,
                EventCategory = s.EventCategory
                
            }).ToList();

        }

        public List<AthleticDataDTO> GetAthleticDataByYearByLevelByAgeGruopByEvent(int year, string levelName, string ageDivision, string eventCategory)
        {
            List<AthleticDataDTO> athleticDataList = new List<AthleticDataDTO>();

            //Get athletic event happened on a year on a particular level on a particular age group on a particular event
            if (year != 0 && levelName != "" && ageDivision != "" && eventCategory != "")
            {

                //Get the athletic Event using age division & event category
                List<hcAthleticEvent> athleticEvents = hcDc.hcAthleticEvents.Where(a => a.AgeDivision == ageDivision && a.EventCategory == eventCategory).ToList();

                //Get only one entry if there are duplicates
                if (athleticEvents.Count >= 0)
                {
                    hcAthleticEvent athleticEvent = athleticEvents.FirstOrDefault();
                    List<hcAthleticData> athleticDataCollection = hcDc.hcAthleticDatas.Where(b => b.AthleticEventId == athleticEvent.AthleticEventId && b.Year == year && b.LevelName == levelName).ToList();


                    foreach (hcAthleticData athleticData in athleticDataCollection)
                    {
                        AthleticDataDTO athleticDataDTO = new AthleticDataDTO();
                        athleticDataDTO.AthleticEventId = athleticEvent.AthleticEventId;
                        athleticDataDTO.EventCategory = athleticEvent.EventCategory;
                        athleticDataDTO.AgeDivision = athleticEvent.AgeDivision;
                        athleticDataDTO.EventName = athleticEvent.AgeDivision + "," + athleticEvent.EventCategory;
                        athleticDataDTO.LevelName = athleticData.LevelName;
                        athleticDataDTO.StudentId = athleticData.StudentId;
                        
                        athleticDataDTO.Place = athleticData.Place;
                        athleticDataDTO.Year = athleticData.Year;
                        athleticDataDTO.NavigationUrl = "viewStudentById.aspx?"+athleticData.StudentId;                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                        athleticDataList.Add(athleticDataDTO);
                    }

                }
                else
                {
                }
            }

             // Get all athletic data a particular year of paricular level of age division
            else if (year != 0 && levelName != "" && ageDivision != "")
            {
                List<hcAthleticEvent> athleticEvents = hcDc.hcAthleticEvents.Where(a => a.AgeDivision == ageDivision).ToList();
                foreach (hcAthleticEvent athleticEvent in athleticEvents)
                {
                    List<hcAthleticData> athleticDataCollection = hcDc.hcAthleticDatas.Where(b => b.AthleticEventId == athleticEvent.AthleticEventId && b.Year == year && b.LevelName == levelName).ToList();


                    foreach (hcAthleticData athleticData in athleticDataCollection)
                    {
                        AthleticDataDTO athleticDataDTO = new AthleticDataDTO();
                        athleticDataDTO.AthleticEventId = athleticEvent.AthleticEventId;
                        athleticDataDTO.EventCategory = athleticEvent.EventCategory;
                        athleticDataDTO.AgeDivision = athleticEvent.AgeDivision;
                        athleticDataDTO.EventName = athleticEvent.AgeDivision + "," + athleticEvent.EventCategory;
                        athleticDataDTO.LevelName = athleticData.LevelName;
                        athleticDataDTO.StudentId = athleticData.StudentId;
                        athleticDataDTO.Place = athleticData.Place;
                        athleticDataDTO.Year = athleticData.Year;
                        athleticDataDTO.NavigationUrl = "viewStudentById.aspx?"+athleticData.StudentId;                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
                        athleticDataList.Add(athleticDataDTO);
                    }
                }
            }

             //Get all age group's athletic data of a particular year of particular level of particular event
            else if (year != 0 && levelName != "" && eventCategory != "")
            {
                List<hcAthleticEvent> athleticEvents = hcDc.hcAthleticEvents.Where(a => a.EventCategory == eventCategory).ToList();
                foreach (hcAthleticEvent athleticEvent in athleticEvents)
                {
                    List<hcAthleticData> athleticDataCollection = hcDc.hcAthleticDatas.Where(b => b.AthleticEventId == athleticEvent.AthleticEventId && b.Year == year && b.LevelName == levelName).ToList();


                    foreach (hcAthleticData athleticData in athleticDataCollection)
                    {
                        AthleticDataDTO athleticDataDTO = new AthleticDataDTO();
                        athleticDataDTO.AthleticEventId = athleticEvent.AthleticEventId;
                        athleticDataDTO.EventCategory = athleticEvent.EventCategory;
                        athleticDataDTO.AgeDivision = athleticEvent.AgeDivision;
                        athleticDataDTO.EventName = athleticEvent.AgeDivision + "," + athleticEvent.EventCategory;
                        athleticDataDTO.LevelName = athleticData.LevelName;
                        athleticDataDTO.StudentId = athleticData.StudentId;
                        athleticDataDTO.Place = athleticData.Place;
                        athleticDataDTO.Year = athleticData.Year;
                        athleticDataDTO.NavigationUrl = "viewStudentById.aspx?"+athleticData.StudentId;                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
                        athleticDataList.Add(athleticDataDTO);
                    }
                }
            }

             //Get all level's athletic data on particular year for particular age group of particular event category
            else if (year != 0 && eventCategory != "" && ageDivision != "")
            {
                List<hcAthleticEvent> athleticEvents = hcDc.hcAthleticEvents.Where(a => a.EventCategory == eventCategory && a.AgeDivision == ageDivision).ToList();
                //Get only one entry if there are duplicates
                if (athleticEvents.Count >= 0)
                {
                    hcAthleticEvent athleticEvent = athleticEvents.FirstOrDefault();
                    List<hcAthleticData> athleticDataCollection = hcDc.hcAthleticDatas.Where(b => b.AthleticEventId == athleticEvent.AthleticEventId && b.Year == year).ToList();


                    foreach (hcAthleticData athleticData in athleticDataCollection)
                    {
                        AthleticDataDTO athleticDataDTO = new AthleticDataDTO();
                        athleticDataDTO.AthleticEventId = athleticEvent.AthleticEventId;
                        athleticDataDTO.EventCategory = athleticEvent.EventCategory;
                        athleticDataDTO.AgeDivision = athleticEvent.AgeDivision;
                        athleticDataDTO.EventName = athleticEvent.AgeDivision + "," + athleticEvent.EventCategory;
                        athleticDataDTO.LevelName = athleticData.LevelName;
                        athleticDataDTO.StudentId = athleticData.StudentId;
                        athleticDataDTO.Place = athleticData.Place;
                        athleticDataDTO.Year = athleticData.Year;
                        athleticDataDTO.NavigationUrl = "viewStudentById.aspx?"+athleticData.StudentId;                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
                        athleticDataList.Add(athleticDataDTO);
                    }

                }
                else
                {
                }
            }

                // Get all year's athletic data of particular level of particular event of particular age division
            else if (levelName != "" && eventCategory != "" && ageDivision != "")
            {
                List<hcAthleticEvent> athleticEvents = hcDc.hcAthleticEvents.Where(a => a.EventCategory == eventCategory && a.AgeDivision == ageDivision).ToList();
                //Get only one entry if there are duplicates
                if (athleticEvents.Count >= 0)
                {
                    hcAthleticEvent athleticEvent = athleticEvents.SingleOrDefault();
                    List<hcAthleticData> athleticDataCollection = hcDc.hcAthleticDatas.Where(b => b.AthleticEventId == athleticEvent.AthleticEventId && b.LevelName == levelName).ToList();


                    foreach (hcAthleticData athleticData in athleticDataCollection)
                    {
                        AthleticDataDTO athleticDataDTO = new AthleticDataDTO();
                        athleticDataDTO.AthleticEventId = athleticEvent.AthleticEventId; 
                        athleticDataDTO.EventCategory = athleticEvent.EventCategory;
                        athleticDataDTO.AgeDivision = athleticEvent.AgeDivision;
                        athleticDataDTO.EventName = athleticEvent.AgeDivision + "," + athleticEvent.EventCategory;
                        athleticDataDTO.LevelName = athleticData.LevelName;
                        athleticDataDTO.StudentId = athleticData.StudentId;
                        athleticDataDTO.Place = athleticData.Place;
                        athleticDataDTO.Year = athleticData.Year;
                        athleticDataDTO.NavigationUrl = "viewStudentById.aspx?"+athleticData.StudentId;                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                        athleticDataList.Add(athleticDataDTO);
                    }

                }
                else
                {
                }
            }

         // Get all data of particular year of particular level 
            else if (year != 0 && levelName != "")
            {
                List<hcAthleticEvent> athleticEvents = hcDc.hcAthleticEvents.ToList();
                foreach (hcAthleticEvent athleticEvent in athleticEvents)
                {
                    List<hcAthleticData> athleticDataCollection = hcDc.hcAthleticDatas.Where(b => b.AthleticEventId == athleticEvent.AthleticEventId && b.Year == year && b.LevelName == levelName).ToList();


                    foreach (hcAthleticData athleticData in athleticDataCollection)
                    {
                        AthleticDataDTO athleticDataDTO = new AthleticDataDTO();
                        athleticDataDTO.AthleticEventId = athleticEvent.AthleticEventId;
                        athleticDataDTO.EventCategory = athleticEvent.EventCategory;
                        athleticDataDTO.AgeDivision = athleticEvent.AgeDivision;
                        athleticDataDTO.EventName = athleticEvent.AgeDivision + "," + athleticEvent.EventCategory;
                        athleticDataDTO.LevelName = athleticData.LevelName;
                        athleticDataDTO.StudentId = athleticData.StudentId;
                        athleticDataDTO.Place = athleticData.Place;
                        athleticDataDTO.Year = athleticData.Year;
                        athleticDataDTO.NavigationUrl = "viewStudentById.aspx?"+athleticData.StudentId;                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
                        athleticDataList.Add(athleticDataDTO);
                    }
                }
            }

             // Get all data of particular year of particular age division
            else if (year != 0 && ageDivision != "")
            {
                List<hcAthleticEvent> athleticEvents = hcDc.hcAthleticEvents.Where(a => a.AgeDivision == ageDivision).ToList();
                foreach (hcAthleticEvent athleticEvent in athleticEvents)
                {
                    List<hcAthleticData> athleticDataCollection = hcDc.hcAthleticDatas.Where(b => b.AthleticEventId == athleticEvent.AthleticEventId && b.Year == year).ToList();


                    foreach (hcAthleticData athleticData in athleticDataCollection)
                    {
                        AthleticDataDTO athleticDataDTO = new AthleticDataDTO();
                        athleticDataDTO.AthleticEventId = athleticEvent.AthleticEventId;
                        athleticDataDTO.EventCategory = athleticEvent.EventCategory;
                        athleticDataDTO.AgeDivision = athleticEvent.AgeDivision;
                        athleticDataDTO.EventName = athleticEvent.AgeDivision + "," + athleticEvent.EventCategory;
                        athleticDataDTO.LevelName = athleticData.LevelName;
                        athleticDataDTO.StudentId = athleticData.StudentId;
                        athleticDataDTO.Place = athleticData.Place;
                        athleticDataDTO.Year = athleticData.Year;
                        athleticDataDTO.NavigationUrl = "viewStudentById.aspx?"+athleticData.StudentId;                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
                        athleticDataList.Add(athleticDataDTO);
                    }
                }
            }

             //Get data for particular year of particular event
            else if (year != 0 && eventCategory != "")
            {
                List<hcAthleticEvent> athleticEvents = hcDc.hcAthleticEvents.Where(a => a.EventCategory == eventCategory).ToList();
                foreach (hcAthleticEvent athleticEvent in athleticEvents)
                {
                    List<hcAthleticData> athleticDataCollection = hcDc.hcAthleticDatas.Where(b => b.AthleticEventId == athleticEvent.AthleticEventId && b.Year == year).ToList();


                    foreach (hcAthleticData athleticData in athleticDataCollection)
                    {
                        AthleticDataDTO athleticDataDTO = new AthleticDataDTO();
                        athleticDataDTO.AthleticEventId = athleticEvent.AthleticEventId;
                        athleticDataDTO.EventCategory = athleticEvent.EventCategory;
                        athleticDataDTO.AgeDivision = athleticEvent.AgeDivision;
                        athleticDataDTO.EventName = athleticEvent.AgeDivision + "," + athleticEvent.EventCategory;
                        athleticDataDTO.LevelName = athleticData.LevelName;
                        athleticDataDTO.StudentId = athleticData.StudentId;
                        athleticDataDTO.Place = athleticData.Place;
                        athleticDataDTO.Year = athleticData.Year;
                        athleticDataDTO.NavigationUrl = "viewStudentById.aspx?"+athleticData.StudentId;                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                        athleticDataList.Add(athleticDataDTO);
                    }
                }
            }
            //Get data of particular level and particular age division
            else if (levelName != "" && ageDivision != "")
            {
                List<hcAthleticEvent> athleticEvents = hcDc.hcAthleticEvents.Where(a => a.AgeDivision == ageDivision).ToList();
                foreach (hcAthleticEvent athleticEvent in athleticEvents)
                {
                    List<hcAthleticData> athleticDataCollection = hcDc.hcAthleticDatas.Where(b => b.AthleticEventId == athleticEvent.AthleticEventId && b.LevelName == levelName).ToList();


                    foreach (hcAthleticData athleticData in athleticDataCollection)
                    {
                        AthleticDataDTO athleticDataDTO = new AthleticDataDTO();
                        athleticDataDTO.AthleticEventId = athleticEvent.AthleticEventId;
                        athleticDataDTO.EventCategory = athleticEvent.EventCategory;
                        athleticDataDTO.AgeDivision = athleticEvent.AgeDivision;
                        athleticDataDTO.EventName = athleticEvent.AgeDivision + "," + athleticEvent.EventCategory;
                        athleticDataDTO.LevelName = athleticData.LevelName;
                        athleticDataDTO.StudentId = athleticData.StudentId;
                        athleticDataDTO.Place = athleticData.Place;
                        athleticDataDTO.Year = athleticData.Year;
                        athleticDataDTO.NavigationUrl = "viewStudentById.aspx?"+athleticData.StudentId;                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                        athleticDataList.Add(athleticDataDTO);
                    }
                }
            }

                //Get all data of particulr age division of event category
            else if (eventCategory != "" && ageDivision != "")
            {
                List<hcAthleticEvent> athleticEvents = hcDc.hcAthleticEvents.Where(a => a.AgeDivision == ageDivision && a.EventCategory == eventCategory).ToList();
                if (athleticEvents.Count >= 0)
                {
                    hcAthleticEvent athleticEvent = athleticEvents.SingleOrDefault();
                    List<hcAthleticData> athleticDataCollection = hcDc.hcAthleticDatas.Where(b => b.AthleticEventId == athleticEvent.AthleticEventId).ToList();


                    foreach (hcAthleticData athleticData in athleticDataCollection)
                    {
                        AthleticDataDTO athleticDataDTO = new AthleticDataDTO();
                        athleticDataDTO.AthleticEventId = athleticEvent.AthleticEventId;
                        athleticDataDTO.EventCategory = athleticEvent.EventCategory;
                        athleticDataDTO.AgeDivision = athleticEvent.AgeDivision;
                        athleticDataDTO.EventName = athleticEvent.AgeDivision + "," + athleticEvent.EventCategory;
                        athleticDataDTO.LevelName = athleticData.LevelName;
                        athleticDataDTO.StudentId = athleticData.StudentId;
                        athleticDataDTO.Place = athleticData.Place;
                        athleticDataDTO.Year = athleticData.Year;
                        athleticDataDTO.NavigationUrl = "viewStudentById.aspx?"+athleticData.StudentId;                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
                        athleticDataList.Add(athleticDataDTO);
                    }

                }
                else
                {
                }
            }

                //Get all data of particular year
            else if (year != 0)
            {
                List<hcAthleticEvent> athleticEvents = hcDc.hcAthleticEvents.ToList();
                foreach (hcAthleticEvent athleticEvent in athleticEvents)
                {
                    List<hcAthleticData> athleticDataCollection = hcDc.hcAthleticDatas.Where(b => b.AthleticEventId == athleticEvent.AthleticEventId && b.Year== year).ToList();


                    foreach (hcAthleticData athleticData in athleticDataCollection)
                    {
                        AthleticDataDTO athleticDataDTO = new AthleticDataDTO();
                        athleticDataDTO.AthleticEventId = athleticEvent.AthleticEventId;
                        athleticDataDTO.EventCategory = athleticEvent.EventCategory;
                        athleticDataDTO.AgeDivision = athleticEvent.AgeDivision;
                        athleticDataDTO.EventName = athleticEvent.AgeDivision + "," + athleticEvent.EventCategory;
                        athleticDataDTO.LevelName = athleticData.LevelName;
                        athleticDataDTO.StudentId = athleticData.StudentId;
                        athleticDataDTO.Place = athleticData.Place;
                        athleticDataDTO.Year = athleticData.Year;
                        athleticDataDTO.NavigationUrl = "viewStudentById.aspx?"+athleticData.StudentId;                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
                        athleticDataList.Add(athleticDataDTO);
                    }
                }
            }
            //Get all data about a level
            else if (levelName != "")
            {
                List<hcAthleticEvent> athleticEvents = hcDc.hcAthleticEvents.ToList();
                foreach (hcAthleticEvent athleticEvent in athleticEvents)
                {
                    List<hcAthleticData> athleticDataCollection = hcDc.hcAthleticDatas.Where(b => b.AthleticEventId == athleticEvent.AthleticEventId && b.LevelName==levelName).ToList();


                    foreach (hcAthleticData athleticData in athleticDataCollection)
                    {
                        AthleticDataDTO athleticDataDTO = new AthleticDataDTO();
                        athleticDataDTO.AthleticEventId = athleticEvent.AthleticEventId;
                        athleticDataDTO.EventCategory = athleticEvent.EventCategory;
                        athleticDataDTO.AgeDivision = athleticEvent.AgeDivision;
                        athleticDataDTO.EventName = athleticEvent.AgeDivision + "," + athleticEvent.EventCategory;
                        athleticDataDTO.LevelName = athleticData.LevelName;
                        athleticDataDTO.StudentId = athleticData.StudentId;
                        athleticDataDTO.Place = athleticData.Place;
                        athleticDataDTO.Year = athleticData.Year;
                        athleticDataDTO.NavigationUrl = "viewStudentById.aspx?"+athleticData.StudentId;                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
                        athleticDataList.Add(athleticDataDTO);
                    }
                }



            }
            //Get all data of particular event eg;100m 
            else if (eventCategory != "")
            {
                List<hcAthleticEvent> athleticEvents = hcDc.hcAthleticEvents.Where(a => a.EventCategory == eventCategory).ToList();
                foreach (hcAthleticEvent athleticEvent in athleticEvents)
                {
                    List<hcAthleticData> athleticDataCollection = hcDc.hcAthleticDatas.Where(b => b.AthleticEventId == athleticEvent.AthleticEventId).ToList();


                    foreach (hcAthleticData athleticData in athleticDataCollection)
                    {
                        AthleticDataDTO athleticDataDTO = new AthleticDataDTO();
                        athleticDataDTO.AthleticEventId = athleticEvent.AthleticEventId;
                        athleticDataDTO.EventCategory = athleticEvent.EventCategory;
                        athleticDataDTO.AgeDivision = athleticEvent.AgeDivision;
                        athleticDataDTO.EventName = athleticEvent.AgeDivision + "," + athleticEvent.EventCategory;
                        athleticDataDTO.LevelName = athleticData.LevelName;
                        athleticDataDTO.StudentId = athleticData.StudentId;
                        athleticDataDTO.Place = athleticData.Place;
                        athleticDataDTO.Year = athleticData.Year;
                        athleticDataDTO.NavigationUrl = "viewStudentById.aspx?"+athleticData.StudentId;                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
                        athleticDataList.Add(athleticDataDTO);
                    }
                }

            }
            else if (ageDivision != "")
            {
                List<hcAthleticEvent> athleticEvents = hcDc.hcAthleticEvents.Where(a => a.AgeDivision == ageDivision).ToList();
                foreach (hcAthleticEvent athleticEvent in athleticEvents)
                {
                    List<hcAthleticData> athleticDataCollection = hcDc.hcAthleticDatas.Where(b => b.AthleticEventId == athleticEvent.AthleticEventId).ToList();


                    foreach (hcAthleticData athleticData in athleticDataCollection)
                    {
                        AthleticDataDTO athleticDataDTO = new AthleticDataDTO();
                        athleticDataDTO.AthleticEventId = athleticEvent.AthleticEventId;
                        athleticDataDTO.EventCategory = athleticEvent.EventCategory;
                        athleticDataDTO.AgeDivision = athleticEvent.AgeDivision;
                        athleticDataDTO.EventName = athleticEvent.AgeDivision + "," + athleticEvent.EventCategory;
                        athleticDataDTO.LevelName = athleticData.LevelName;
                        athleticDataDTO.StudentId = athleticData.StudentId;
                        athleticDataDTO.Place = athleticData.Place;
                        athleticDataDTO.Year = athleticData.Year;
                        athleticDataDTO.NavigationUrl = "viewStudentById.aspx?"+athleticData.StudentId;                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
                        athleticDataList.Add(athleticDataDTO);
                    }
                }
            }


            List<AthleticDataDTO> athlticDataDTO = new List<AthleticDataDTO>();
            athlticDataDTO.AddRange(athleticDataList.Where(a => a.Place == "First").ToList());

            athlticDataDTO.AddRange(athleticDataList.Where(a => a.Place == "Second").ToList());

            athlticDataDTO.AddRange(athleticDataList.Where(a => a.Place == "Third").ToList());

            athlticDataDTO.AddRange(athleticDataList.Where(a => a.Place == "Fourth").ToList());

            athlticDataDTO.AddRange(athleticDataList.Where(a => a.Place == "Fifth").ToList());

            athlticDataDTO.AddRange(athleticDataList.Where(a => a.Place == "Participation").ToList());

            return athleticDataList;
        }


        public List<AthleticDataDTO> GetAllAthleticData()
        {



            var mydata = from tc in hcDc.hcAthleticDatas
                         group tc by new { a = tc.Year, b = tc.LevelName , c=tc.AthleticEventId  } into grp
                         select new AthleticDataDTO {Year= grp.Key.a, LevelName=grp.Key.b };
        
                  //new dto{}          
            //return mydata.ToList();      

            List<AthleticDataDTO> athlticDataDTO = new List<AthleticDataDTO>();
            athlticDataDTO.AddRange(hcDc.hcAthleticDatas.Where(a => a.Place == "First").Select(s => new AthleticDataDTO
            {
                AthleticEventId = s.AthleticEventId,
                LevelName = s.LevelName,
                NavigationUrl = "",
                Place = s.Place,
                StudentId = s.StudentId,
                Year = s.Year,
                
            }).ToList());

            athlticDataDTO.AddRange(hcDc.hcAthleticDatas.Where(a => a.Place == "Second").Select(s => new AthleticDataDTO
            {
                AthleticEventId = s.AthleticEventId,
                LevelName = s.LevelName,
                NavigationUrl = "",
                Place = s.Place,
                StudentId = s.StudentId,
                Year = s.Year,

            }).ToList());

            athlticDataDTO.AddRange(hcDc.hcAthleticDatas.Where(a => a.Place == "Third").Select(s => new AthleticDataDTO
            {
                AthleticEventId = s.AthleticEventId,
                LevelName = s.LevelName,
                NavigationUrl = "",
                Place = s.Place,
                StudentId = s.StudentId,
                Year = s.Year,

            }).ToList());

            athlticDataDTO.AddRange(hcDc.hcAthleticDatas.Where(a => a.Place == "Fourth").Select(s => new AthleticDataDTO
            {
                AthleticEventId = s.AthleticEventId,
                LevelName = s.LevelName,
                NavigationUrl = "",
                Place = s.Place,
                StudentId = s.StudentId,
                Year = s.Year,

            }).ToList());

            athlticDataDTO.AddRange(hcDc.hcAthleticDatas.Where(a => a.Place == "Fifth").Select(s => new AthleticDataDTO
            {
                AthleticEventId = s.AthleticEventId,
                LevelName = s.LevelName,
                NavigationUrl = "",
                Place = s.Place,
                StudentId = s.StudentId,
                Year = s.Year,

            }).ToList());

            athlticDataDTO.AddRange(hcDc.hcAthleticDatas.Where(a => a.Place == "Participation").Select(s => new AthleticDataDTO
            {
                AthleticEventId = s.AthleticEventId,
                LevelName = s.LevelName,
                NavigationUrl = "",
                Place = s.Place,
                StudentId = s.StudentId,
                Year = s.Year,

            }).ToList());

           /* return hcDc.hcAthleticDatas.Select(s => new AthleticDataDTO
            {
                AthleticEventId = s.AthleticEventId,
                LevelName = s.LevelName,
                NavigationUrl = "",
                Place = s.Place,
                StudentId = s.StudentId,
                Year = s.Year,
                
            }).ToList();*/

            return athlticDataDTO;
        }

        public List<AthleticDataDTO> GetAllYrsOfAthleticData()
        {
            return hcDc.hcAthleticDatas.Select(s => new AthleticDataDTO
            {
                AthleticEventId = s.AthleticEventId,
                LevelName = s.LevelName,
                NavigationUrl = "",
                Place = s.Place,
                StudentId = s.StudentId,
                Year = s.Year
            }).Distinct().ToList();
        }


        public List<AthleticDataDTO> GetAllLevelsOfAthleticData()
        {
            List<AthleticDataDTO> athleticDta = new List<AthleticDataDTO>();
            athleticDta.AddRange(hcDc.hcAthleticDatas.Where(a=>a.LevelName=="InterHouse").Select(s => new AthleticDataDTO
            {
                AthleticEventId = s.AthleticEventId,
                LevelName = s.LevelName,
                NavigationUrl = "",
                Place = s.Place,
                StudentId = s.StudentId,
                Year = s.Year
            }).Distinct().ToList());

            athleticDta.AddRange(hcDc.hcAthleticDatas.Where(a => a.LevelName == "Department Level").Select(s => new AthleticDataDTO
            {
                AthleticEventId = s.AthleticEventId,
                LevelName = s.LevelName,
                NavigationUrl = "",
                Place = s.Place,
                StudentId = s.StudentId,
                Year = s.Year
            }).Distinct().ToList());

            athleticDta.AddRange(hcDc.hcAthleticDatas.Where(a => a.LevelName == "Zonal Level").Select(s => new AthleticDataDTO
            {
                AthleticEventId = s.AthleticEventId,
                LevelName = s.LevelName,
                NavigationUrl = "",
                Place = s.Place,
                StudentId = s.StudentId,
                Year = s.Year
            }).Distinct().ToList());

            athleticDta.AddRange(hcDc.hcAthleticDatas.Where(a => a.LevelName == "Distirct Level").Select(s => new AthleticDataDTO
            {
                AthleticEventId = s.AthleticEventId,
                LevelName = s.LevelName,
                NavigationUrl = "",
                Place = s.Place,
                StudentId = s.StudentId,
                Year = s.Year
            }).Distinct().ToList());

            athleticDta.AddRange(hcDc.hcAthleticDatas.Where(a => a.LevelName == "Provinicial Level").Select(s => new AthleticDataDTO
            {
                AthleticEventId = s.AthleticEventId,
                LevelName = s.LevelName,
                NavigationUrl = "",
                Place = s.Place,
                StudentId = s.StudentId,
                Year = s.Year
            }).Distinct().ToList());

            athleticDta.AddRange(hcDc.hcAthleticDatas.Where(a => a.LevelName == "National Level").Select(s => new AthleticDataDTO
            {
                AthleticEventId = s.AthleticEventId,
                LevelName = s.LevelName,
                NavigationUrl = "",
                Place = s.Place,
                StudentId = s.StudentId,
                Year = s.Year
            }).Distinct().ToList());

            athleticDta.AddRange(hcDc.hcAthleticDatas.Where(a => a.LevelName == "International Level").Select(s => new AthleticDataDTO
            {
                AthleticEventId = s.AthleticEventId,
                LevelName = s.LevelName,
                NavigationUrl = "",
                Place = s.Place,
                StudentId = s.StudentId,
                Year = s.Year
            }).Distinct().ToList());

            athleticDta.AddRange(hcDc.hcAthleticDatas.Where(a => a.LevelName == "Other").Select(s => new AthleticDataDTO
            {
                AthleticEventId = s.AthleticEventId,
                LevelName = s.LevelName,
                NavigationUrl = "",
                Place = s.Place,
                StudentId = s.StudentId,
                Year = s.Year
            }).Distinct().ToList());
            return athleticDta;
        }

        public List<AthleticEventDTO> GetAllAgeDivisionAndEventCategoryByYear(int yr)
        {
            List<AthleticEventDTO> athleticDataByAgeDivision = new List<AthleticEventDTO>();
            List<Guid> AllAthleticEventId = hcDc.hcAthleticDatas.Where(b=>b.Year ==yr).Select(a => a.AthleticEventId).ToList();
            foreach (Guid athleticEvntId in AllAthleticEventId)
            {
                AthleticEventDTO athleticEventDTO = new AthleticEventDTO();
                List<hcAthleticEvent> athleticEvents = hcDc.hcAthleticEvents.Where(b=>b.AthleticEventId== athleticEvntId ).ToList();
                foreach(hcAthleticEvent athleticEvent in athleticEvents)
                {
                    athleticEventDTO.AgeDivision = athleticEvent.AgeDivision;
                    athleticEventDTO.EventCategory = athleticEvent.EventCategory;
                    athleticDataByAgeDivision.Add(athleticEventDTO);
                }
            }
            return athleticDataByAgeDivision;
        }


        public List<AthleticEventDTO> GetAllAgeDivisionAndEventCategoryFromAthleticDataList(List<AthleticDataDTO> athleticData)
        {
            List<AthleticEventDTO> athleticDataByAgeDivision = new List<AthleticEventDTO>();
            List<Guid> AllAthleticEventId = athleticData.Select(a => a.AthleticEventId).ToList();
            foreach (Guid athleticEvntId in AllAthleticEventId)
            {
                AthleticEventDTO athleticEventDTO = new AthleticEventDTO();
                hcAthleticEvent athleticEvents = hcDc.hcAthleticEvents.Where(b => b.AthleticEventId == athleticEvntId).SingleOrDefault();
                
                
                    
                    athleticEventDTO.AgeDivision = athleticEvents.AgeDivision;
                    athleticEventDTO.EventCategory = athleticEvents.EventCategory;
                    athleticEventDTO.EventName = athleticEvents.AgeDivision + "," + athleticEvents.EventCategory;
                    athleticDataByAgeDivision.Add(athleticEventDTO);
                
            }
            return athleticDataByAgeDivision;
        }

        public List<Guid> GetAthleticIdByAgeDivisionAndEventCategory(string ageDivision,string eventCategory)
        {
            
            List<Guid> AthleticEventId = hcDc.hcAthleticEvents.Where(b=>b.AgeDivision==ageDivision && b.EventCategory==eventCategory).Select(a => a.AthleticEventId).ToList();

            return AthleticEventId;
        }



        public List<AthleticDataDTO> GetAllAthleticDataOfStudent(Guid studentId)
        {
            return hcDc.hcAthleticDatas.Where(a=>a.StudentId == studentId).Select(s => new AthleticDataDTO
            {
                AthleticEventId = s.AthleticEventId,
                LevelName = s.LevelName,
                NavigationUrl = "",
                Place = s.Place,
                StudentId = s.StudentId,
                Year = s.Year
            }).Distinct().ToList();
        }

        //public bool isRelay(Guid athleticEventId)
        //{
        //    return hcDc.hcAthleticEvents.Where(a => a.AthleticEventId == athleticEventId).SingleOrDefault().IsRelay;
        //}
    }
}
